﻿using System;
using System.Windows.Forms;
using SdmxMl.Structure;

namespace SilcQuest
{
    /// <summary>
    /// Edit dialog for detailled history suivi Item.
    /// </summary>
    public partial class DlgSuivi : Form
    {
        #region Private members

        private CatiSuiviItem suiviItem;

        #endregion

        #region C'tor

        /// <summary></summary>
        /// <param name="csi">the history item</param>
        /// <param name="readOnly">Do dialog present a read only view</param>
        public DlgSuivi(CatiSuiviItem csi, bool readOnly)
        {
            suiviItem = csi;
            InitializeComponent();

            // Fill text
            textBoxProblem.Text = suiviItem.Problem;
            textBoxId.Text = suiviItem.Id;
            textBoxDate.Text = suiviItem.Date;

            // Fill the combo
            comboBoxLang.Items.AddRange(suiviItem.GetCodelistOfField("LANG").CodeList.ToArray());
            comboBoxUserId.Items.AddRange(suiviItem.GetCodelistOfField("USER").CodeList.ToArray());
            comboBoxTrStatus.Items.AddRange(suiviItem.GetCodelistOfField("TR_STAT").CodeList.ToArray());
            comboBoxField.Items.AddRange(suiviItem.GetCodelistOfField("FIELD_TYPE").CodeList.ToArray());

            //Select defined
            BindToValue(comboBoxLang, suiviItem.LangVal);
            BindToValue(comboBoxUserId, suiviItem.UserVal);
            BindToValue(comboBoxTrStatus, suiviItem.TranslationStatusVal);
            BindToValue(comboBoxField, suiviItem.FieldTypeVal);


            if (readOnly)
            {
                textBoxProblem.ReadOnly = true;
                textBoxId.ReadOnly = true;
                comboBoxLang.Enabled = false;
                comboBoxUserId.Enabled = false;
                comboBoxTrStatus.Enabled = false;
                comboBoxField.Enabled = false;
                buttonSave.Enabled = false;
            }
        }

        private void BindToValue(ComboBox cb, string val)
        {
            foreach (CodeType c in cb.Items)
            {
                if (c != null && c.Id == val)
                {
                    cb.SelectedItem = c;
                    break;
                }
            }
        }
        #endregion

        #region Events

        private void DlgSuivi_FormClosing(object sender, FormClosingEventArgs e)
        {
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            bool hasModif = false;

            if (suiviItem.Problem != textBoxProblem.Text)
            {
                hasModif = true;
                suiviItem.Problem = textBoxProblem.Text;
            }

            CodeType code = comboBoxLang.SelectedItem as CodeType;
            string codeVal = code == null ? string.Empty : code.Id;
            if (suiviItem.LangVal != codeVal)
            {
                hasModif = true;
                suiviItem.LangVal = codeVal;
            }

            code = comboBoxUserId.SelectedItem as CodeType;
            codeVal = code == null ? string.Empty : code.Id;
            if (suiviItem.UserVal != codeVal)
            {
                hasModif = true;
                suiviItem.UserVal = codeVal;
            }

            code = comboBoxTrStatus.SelectedItem as CodeType;
            codeVal = code == null ? string.Empty : code.Id;
            if (suiviItem.TranslationStatusVal != codeVal)
            {
                hasModif = true;
                suiviItem.TranslationStatusVal = codeVal;
            }

            code = comboBoxField.SelectedItem as CodeType;
            codeVal = code == null ? string.Empty : code.Id;
            if (suiviItem.FieldTypeVal != codeVal)
            {
                hasModif = true;
                suiviItem.FieldTypeVal = codeVal;
            }

            DialogResult = hasModif ? DialogResult.OK : DialogResult.Cancel;
            this.Close();

        }

        private void DlgSuivi_Load(object sender, EventArgs e)
        {
            textBoxProblem.SelectionStart = textBoxProblem.SelectionLength = 0;
        }
        #endregion
    }
}
